#include "optionadd.h"
#include "ui_optionadd.h"
void OptionAdd::setTime(QDateTime time){
    this->time = time;
}
OptionAdd::OptionAdd(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::OptionAdd)
{
    this->time = QDateTime::currentDateTime();
    ui->setupUi(this);
    showFullScreen();
    connect(ui->foodSelect,&FoodSelect::selected,this,[=](Food food){
        this->selected = food;
        this->haveSelected = true;
        ui->label_name->setText("食物:"+food.name);
    });
    connect(ui->btn_back,&QToolButton::clicked,[=](){
        this->close();
        //    ui->stackedWidget->setCurrentIndex(ui->stackedWidget->currentIndex()+1);
    });
    connect(ui->btn_direction,&QToolButton::clicked,[=](){
        ui->btn_direction->setText(ui->btn_direction->text()=="入"?"出":"入");
    });
    connect(ui->btn_complete,&QToolButton::clicked,[=](){
        if (haveSelected&&(ui->amount->text()!=NULL&&ui->amount->text()!="")){
            int amount = ui->amount->text().toInt();
            if (amount<=0){
                QMessageBox::critical(this, tr("存储添加错误"), tr("请输入正确的数量，且保证数量>0"));
            }
            else{
                Option option;
                option.user_id = this->selected.create_user;
                option.food_id = this->selected.create_id;
                option.status = 0;
                option.amount = amount;
                option.direction = ui->btn_direction->text()=="入"?1:0;
                option.datetime = this->time;
                sql.optionAdd(option);

                qDebug()<<"成功添加";
                emit completed();
                this->close();

            }


        }
        // 添加成功
    });
}

OptionAdd::~OptionAdd()
{
    delete ui;
}
